home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / appkit.642 < prev    next >
Text File  |  1992-02-06  |  2KB  |  49 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f1\fswiss Helvetica;\f2\fmodern Courier;}
  2. \paperw11520
  3. \paperh7620
  4. \margl120
  5. \margr120
  6. {\colortbl\red0\green0\blue0;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ul0\fs28 application pathname executable\
  8. \
  9. Q:  How do I get the full pathname of the executable file's directory from within my application after it has been launched?\
  10. \
  11. A:  Here is a sample function that does this.  If the application is launched from the Workspace, NXArgv[0] will contain the full pathname of the executable file.  If you launch the application from a terminal command line,  NXArgv[0] will contain only the filename specified in the command.  To get from that filename to the absolute path of its directory, you can use a combination of which, chdir() and getwd() .\
  12.  
  13. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\f1\fs24\fc0 \
  14. \
  15.  
  16. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f2\fc0 #import <appkit/defaults.h>      // for NXArgv\
  17. #import <libc.h>        // for chdir, getwd\
  18. \
  19. void getAppDirectory (char *appDirectory)\
  20. \{\
  21.     FILE *process;\
  22.     char command[256];\
  23.     char *suffix;\
  24. \
  25.     strcpy (appDirectory,NXArgv[0]);\
  26.     if (appDirectory[0] == '/') \{         /* if absolute path */\
  27.         if (suffix = rindex(appDirectory,'/')) \
  28.             *suffix  = '\\0';             /* remove executable name */\
  29.     \} else \{\
  30.     sprintf(command,"which '%s'\\n",NXArgv[0]);\
  31.     process=popen(command,"r");\
  32.     fscanf(process,"%s",appDirectory);\
  33.     pclose(process);\
  34.     if (suffix = rindex(appDirectory,'/')) \
  35.         *suffix  = '\\0';             /* remove executable name */\
  36.     chdir(appDirectory);\
  37.     getwd(appDirectory);\
  38.     \}  \
  39. \}\
  40.  
  41. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28 \
  42. QA642\
  43. \
  44.  
  45. \pard\tx533\tx1067\tx1601\tx2135\tx2668\tx3202\tx3736\tx4270\tx4803\tx5337\fc0 Valid for 2.0
  46. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600 \
  47. \
  48.  
  49.